/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.ban.ycl.bk.e2p.ysyb.tools;

import cn.ac.iie.di.ban.ycl.bk.e2p.ysyb.main.BanBkE2p;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;

/**
 *
 * @author wmr
 */
public class LcUtil {
    
    private static Logger logger = Logger.getLogger(LcUtil.class);
    
    /**
     * 复制单个文件
     *
     * @param oldPath String 原文件路径 如：c:/fqf.txt
     * @param newPath String 复制后路径 如：f:/fqf.txt
     * @return boolean
     */
    public boolean copyFile(String oldPath, String newPath) {
        boolean copyFlag = true;
        File from = new File(oldPath);
        File to = new File(newPath);
        try {
            FileUtils.copyFile(from, to);
            
//            chmodForFolder(newPath);
        } catch (Exception e) {
            copyFlag = false;
            logger.error("复制文件并赋权限操作出错" + e.getMessage(), e);
            
        }finally{
            return copyFlag;
        }
    }

    public void chmodForFolder(String fileName) throws IOException {

        logger.info("chmodForFile:" + fileName);
        String[] filepath = fileName.split(File.separator);
        logger.info(fileName + " .length()= " + fileName.length());
        File wd = new File("/bin");
        Process proc = null;
        try {
            proc = Runtime.getRuntime().exec("/bin/bash", null, wd);
        } catch (IOException e) {
            logger.error("Runtime.getRuntime(): " + e.getMessage(), e);
        }
        if (proc != null) {
            PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())), true);
//            out.println("cd /ysb");
//            logger.info("cd /ysb");
            out.println("cd " + BanBkE2p.lcBasePath);
            logger.info("cd " + BanBkE2p.lcBasePath);
            for (int i = 3; i < filepath.length; i++) {
                out.println("chmod 777 " + filepath[i]);
                if (i == filepath.length - 1) {
                    logger.info("file ---------------------" + filepath[i]);
                    break;
                } else {
                    out.println("cd " + filepath[i]);
                    logger.info("cd " + filepath[i] + "*****************");
                }

            }
//            out.println("chmod -R 777 " + filepath[3]);
//            logger.info("chmod -R 777 " + filepath[3]);
            out.println("exit");
            logger.info("chmod exit !!!");
            try {

                StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");
                // kick off stderr  
                errorGobbler.run();
                StreamGobbler outGobbler = new StreamGobbler(proc.getInputStream(), "STDOUT");
                // kick off stdout  
                outGobbler.run();

                logger.debug("wait ...");
                int waitFor = proc.waitFor();
                logger.info("return proc.waitFor(): " + waitFor);
                out.close();
                proc.destroy();
            } catch (Exception e) {
                logger.error("chmod -R 777 error: " + e.getMessage(), e);
            }
        }

    }
    
}
